package com.easyapi.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author javadadi
 */
@Configuration
public class RabbitmqConfig {

    // 定义常量
    public static final String DATASOURCE_QUEUE = "DatasourceDirectQueue";
    public static final String API_QUEUE = "ApiDirectQueue";
    public static final String DIRECT_EXCHANGE = "EasyApiDirectExchange";
    public static final String LONELY_DIRECT_EXCHANGE = "lonelyDirectExchange";
    public static final String DATASOURCE_ROUTING_KEY = "DatasourceDirectRouting";
    public static final String API_ROUTING_KEY = "ApiDirectRouting";

    // 队列命名：DatasourceDirectQueue
    @Bean
    public Queue DatasourceDirectQueue() {
        return new Queue(DATASOURCE_QUEUE, true);
    }

    // 队列命名：ApiDirectQueue
    @Bean
    public Queue ApiDirectQueue() {
        return new Queue(API_QUEUE, true);
    }

    // Direct 交换机命名：EasyApiDirectExchange
    @Bean
    public DirectExchange EasyApiDirectExchange() {
        return new DirectExchange(DIRECT_EXCHANGE, true, false);
    }

    // 将 Datasource 队列与交换机绑定，并设置 routingKey: DatasourceDirectRouting
    @Bean
    public Binding bindingDatasourceDirect() {
        return BindingBuilder.bind(DatasourceDirectQueue())
                .to(EasyApiDirectExchange())
                .with(DATASOURCE_ROUTING_KEY);
    }

    // 将 API 队列与交换机绑定，并设置 routingKey: ApiDirectRouting
    @Bean
    public Binding bindingApiDirect() {
        return BindingBuilder.bind(ApiDirectQueue())
                .to(EasyApiDirectExchange())
                .with(API_ROUTING_KEY);
    }

    // 创建一个独立的 Direct 交换机，没有与队列绑定
    @Bean
    public DirectExchange lonelyDirectExchange() {
        return new DirectExchange(LONELY_DIRECT_EXCHANGE);
    }

}
